package cn.picclife.mwx.manager.sys.quartz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.picclife.mwx.manager.sys.quartz.domain.SysJob;
import cn.picclife.mwx.manager.sys.quartz.service.ISysJobService;
import cn.picclife.mwx.manager.sys.quartz.util.AjaxResult;
import cn.picclife.mwx.manager.sys.quartz.util.CronUtils;
import cn.picclife.mwx.manager.sys.quartz.util.ExcelUtil;
import cn.picclife.mwx.manager.sys.quartz.util.Log;
import cn.picclife.mwx.manager.sys.quartz.util.TaskException;
import cn.picclife.mwx.manager.sys.quartz.util.enums.BusinessType;
import cn.picclife.mwx.manager.sys.quartz.util.page.TableDataInfo;

/**
 * 调度任务信息操作处理
 * 
 * @author 
 */
@Controller
@RequestMapping("/monitor/job")
public class SysJobController extends BaseController {
	
	private String prefix = "monitor/job";
    @Autowired
    private ISysJobService jobService;

    /**
     * 任务列表查询
     * @param job
     * @return
     */
	@RequestMapping(value="/list", method= {RequestMethod.POST})
    @ResponseBody
    public TableDataInfo list(SysJob job) {
        startPage();
        List<SysJob> list = jobService.selectJobList(job);
        return getDataTable(list);
    }

	/**
     * 任务调度删除
     */
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
	@RequestMapping(value="/remove", method= {RequestMethod.POST})
    @ResponseBody
    public AjaxResult remove(String ids) throws SchedulerException {
        jobService.deleteJobByIds(ids);
        return success();
    }
    
    /**
     * 任务调度状态修改
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequestMapping(value="/changeStatus", method= {RequestMethod.POST})
    @ResponseBody
    public AjaxResult changeStatus(SysJob job) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return toAjax(jobService.changeStatus(newJob));
    }
	
    /**
	 * 详情
	 * @param jobId Long.valueOf(jobId)
	 * @return
	 */
	@RequestMapping(value="/detail", method= {RequestMethod.POST})
	@ResponseBody
    public SysJob detail(Long jobId) {
		SysJob jop = jobService.selectJobById(jobId);
        return jop;
    }
    
	/**
     * 任务调度立即执行一次
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequestMapping(value="/run", method= {RequestMethod.POST})
    @ResponseBody
    public AjaxResult run(SysJob job) throws SchedulerException {
        jobService.run(job);
        return success();
    }
//    @Log(title = "定时任务导出", businessType = BusinessType.EXPORT)
//	@RequestMapping(value="/export", method= {RequestMethod.POST})
//    @ResponseBody
//    public AjaxResult export(SysJob job)
//    {
//        List<SysJob> list = jobService.selectJobList(job);
//        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
//        return util.exportExcel(list, "定时任务");
//    }


//	@RequestMapping(value="/detail/{jobId}", method= {RequestMethod.GET})
//    public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap)
//    {
//        mmap.put("name", "job");
//        mmap.put("job", jobService.selectJobById(jobId));
//        return prefix + "/detail";
//    }@RequestParam("jobId")
	

    

//    /**
//     * 新增调度
//     */
//	@RequestMapping(value="/add", method= {RequestMethod.GET})
//    public String add() {
//        return prefix + "/add";
//    }

    /**
     * 新增保存调度
     */
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
	@RequestMapping(value="/add", method= {RequestMethod.POST})
    @ResponseBody
    public AjaxResult addSave(@RequestBody SysJob job,HttpServletRequest request) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())){
            return AjaxResult.error("cron表达式不正确");
        }
        return toAjax(jobService.insertJob(job));
    }

//    /**
//     * 修改调度
//     */
//	@RequestMapping(value="/edit/{jobId}", method= {RequestMethod.GET})
//    public String edit(@PathVariable("jobId") String jobId, ModelMap mmap) {
//        mmap.put("job", jobService.selectJobById(jobId));
//        return prefix + "/edit";
//    }

    /**
     * 修改保存调度
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequestMapping(value="/edit", method= {RequestMethod.POST})
    @ResponseBody
    public AjaxResult editSave(@RequestBody SysJob job, HttpServletRequest request) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return AjaxResult.error("cron表达式不正确");
        }
        return toAjax(jobService.updateJob(job));
    }

    /**
     * 校验cron表达式是否有效
     */
	@RequestMapping(value="/checkCronExpressionIsValid", method= {RequestMethod.POST})
    @ResponseBody
    public boolean checkCronExpressionIsValid(SysJob job)
    {
        return jobService.checkCronExpressionIsValid(job.getCronExpression());
    }
}
